package cate.game.play.support;

import cate.common.table.d.GDFight;
import cate.game.play.fighter.HeroFighter;
import cate.game.play.part.FightTeam;
import cate.game.play.proce.play.PlayPO;
import lombok.experimental.UtilityClass;
import org.springframework.util.CollectionUtils;

import java.util.Objects;
import java.util.function.Function;

/**
 * 剧本辅助API
 */
@UtilityClass
public final class PlaySupport {
    // 输出
    static final Function<HeroFighter, Long> DAMAGE_MAPPER = (fighter) -> fighter.getPerformance().damage;
    // 治疗
    static final Function<HeroFighter, Long> HEAL_MAPPER = (fighter) -> fighter.getPerformance().heal;
    // 承伤
    static final Function<HeroFighter, Long> HURT_MAPPER = (fighter) -> fighter.getPerformance().hurt;

    /**
     * 统计输出
     *
     * @param playPO
     * @param side
     * @return
     */
    public static long countDamage(PlayPO playPO, byte side) {
        return count(playPO, side, DAMAGE_MAPPER);
    }

    /**
     * 统计治疗
     *
     * @param playPO
     * @param side
     * @return
     */
    public static long countHeal(PlayPO playPO, byte side) {
        return count(playPO, side, HEAL_MAPPER);
    }

    /**
     * 统计承伤
     *
     * @param playPO
     * @param side
     * @return
     */
    public static long countHurt(PlayPO playPO, byte side) {
        return count(playPO, side, HURT_MAPPER);
    }

    static long count(PlayPO playPO, byte side, Function<HeroFighter, Long> mapper) {
        if (Objects.isNull(playPO) || side == GDFight.Team.NONE) {
            return 0L;
        }

        FightTeam team = (side == GDFight.Team.A) ? playPO.teamEnd.a : playPO.teamEnd.b;
        return Objects.isNull(team) || CollectionUtils.isEmpty(team.getAllHero())
                ? 0L
                : team.getAllHero().stream().map(mapper).count();
    }

}
